<template>
  <view class="page">
    <view
      class="item"
      :class="{ 'is-warning': item.materialBuyId !== null }"
      v-for="(item, index) in list"
      :key="index"
    >
      <view class="item-type flex flex-between">
        <view class="flex flex-row">
          <view>{{ item.taskName }}</view>
          <view class="item-tips">{{
            item.materialBuyId == !null ? "(需业主采买)" : ""
          }}</view>
        </view>

        <wd-tag
          color="#FF9900"
          bg-color="#FFF0D9"
          v-if="item.materialBuyId !== null"
          >自购</wd-tag
        >
      </view>
      <view class="item-text">{{ item.buyRemark }}</view>
      <view class="item-state flex flex-between">
        <view
          class="item-expect"
          v-if="item.overdueStatus == 0 && item.purchaseTime !== null"
        >
          预计在{{ item.purchaseTime }}前，发起采买</view
        >
        <view
          class="overdue"
          v-if="item.overdueStatus == 1 && item.purchaseTime !== null"
        >
          预计在{{ item.purchaseTime }}前，发起采买</view
        >
        <view
          class="item-btn btn-info"
          v-if="item.isAlreadyBuy == 1 && item.materialBuyId !== null"
          >已采买</view
        >
        <view
          class="item-btn"
          v-if="item.isAlreadyBuy == 0 && item.materialBuyId !== null"
          @click="finishPlan(item)"
        >
          我已完成自采</view
        >
      </view>
      <view class="item-divider"></view>
      <view class="item-state flex flex-between">
        <view class="state" v-if="item.status == 1">未开始</view>
        <view class="flex flex-row" v-if="item.status == 2">
          <view class="primary">进行中</view>
          <view class="state-time" style="margin: 0 10rpx">·</view>
          <view class="state-time"> 预计{{ item.startDate }}完工</view>
        </view>
        <view class="flex flex-row" v-if="item.status == 3">
          <view class="success">已完成</view>
          <view class="state" style="margin: 0 10rpx">·</view>
          <view class="state"> {{ item.startDate }}</view>
        </view>
      </view>
    </view>
    <view class="header__phone" @click.stop="handlePhone">
      <image
        class="header__phone__img"
        src="http://51-jzt-dev.oss-cn-hangzhou.aliyuncs.com/miniapp/contact-phone.png"
      />
      电话咨询
    </view>
    <view class="reach-bottom" v-if="hasMore">{{
      loading ? "加载中..." : "上拉加载更多"
    }}</view>
    <view class="reach-bottom" v-else>没有更多数据了</view>

    <phone-popup v-if="showPhonePopup" v-model="showPhonePopup"></phone-popup>
  </view>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { onReachBottom } from "@dcloudio/uni-app";
import PhonePopup from "../user/components/PhonePopup/PhonePopup.vue";
import { getTaskList, completeBuy } from "@/api/selfPlan";
const list = ref([]); // 列表数据
const page = ref(1); // 当前页数
const pageSize = ref(10); // 每页条数
const total = ref(50); // 每页条数
const hasMore = ref(true); // 是否有更多数据
const loading = ref(false);

/**
 * 拨打电话
 */
const showPhonePopup = ref(false);
const handlePhone = () => {
  showPhonePopup.value = true;
};
const finishPlan = async (v) => {
  let obj = {
    materialBuyId: v.materialBuyId,
  };
  const { data, code } = await completeBuy(obj);
  code == 200 && loadData();
};
onReachBottom(() => {
  if (hasMore.value) {
    loadMore();
  }
});

const loadData = () => {
  getTaskList().then((res) => {
    list.value = res.data;
  });
};

const loadMore = () => {
  if (page.value * pageSize.value >= total.value) {
    hasMore.value = false;
    uni.showToast({
      title: "没有更多数据了",
      icon: "none",
    });
    return;
  }
  page.value++;
  loadData();
};
onMounted(() => {
  loadData();
});
</script>

<style scoped lang="scss">
$primary: #007dff;
$success: #0ac905;
$warning: #ffb73b;
$error: #ff3333;
$info: #86909c;

.page {
  padding: 32rpx;
  padding-bottom: 0;

  .item + .item {
    margin-top: 20rpx;
  }

  .item {
    width: calc(100% - 16rpx - 48rpx);
    background: #ffffff;
    box-shadow: 0px 0 20px 0px rgba(134, 144, 156, 0.1);
    border-radius: 20rpx;
    border-left: 16rpx solid $primary;
    padding: 24rpx;

    &.is-warning {
      border-left: 16rpx solid $warning;
    }

    .item-type {
      font-size: 24rpx;
      color: #4e5969;

      .item-tips {
        font-size: 20rpx;
        color: $info;
      }
    }

    .item-text {
      padding: 20rpx;
      font-size: 20rpx;
      color: #3d3d3d;
      line-height: 40rpx;
      background: #f6f8fa;
      border-radius: 10px;
      margin-top: 10rpx;
    }

    .item-expect {
      font-size: 20rpx;
      color: $info;
      margin-top: 10rpx;
    }

    .item-divider {
      margin-top: 20rpx;
      height: 2rpx;
      background-color: #f5f7fa;
    }

    .item-state {
      margin-top: 10rpx;

      .state {
        font-size: 20rpx;
      }

      .primary {
        color: $primary;
        font-size: 20rpx;
      }

      .success {
        color: $success;
        font-size: 20rpx;
      }

      .state-time {
        font-size: 20rpx;
        color: $info;
      }

      .overdue {
        font-size: 20rpx;
        color: $error;
      }

      .item-btn {
        width: 204rpx;
        height: 48rpx;
        line-height: 48rpx;
        background: $primary;
        border-radius: 28px;
        text-align: center;
        font-size: 28rpx;
        color: #ffffff;

        &.btn-info {
          width: 148rpx;
          background: #dcdfe6;
          margin-right: 26rpx;
        }
      }
    }
  }

  .header__phone {
    background: #ffffff;
    box-shadow: 0rpx 16rpx 16rpx 0rpx rgba(66, 78, 102, 0.1);
    border-radius: 35rpx 0rpx 0rpx 35rpx;
    border: 2rpx solid #c2d4e8;
    padding: 15rpx 12rpx;
    position: fixed;
    right: 0rpx;
    // top: 50%;
    // transform: translateY(-50%);
    bottom: 122rpx;
    display: flex;
    align-items: center;
    font-size: 24rpx;
    color: #3d3d3d;

    .header__phone__img {
      width: 40rpx;
      height: 40rpx;
      display: inline-block;
      margin-right: 5px;
    }
  }

  .reach-bottom {
    text-align: center;
    height: 80rpx;
    line-height: 80rpx;
    font-size: 24rpx;
    color: $info;
  }
}
</style>
